﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Xml;

namespace AIC_CD
{
    public partial class BaoCaoSoLieuTinhHinhHoatDongCongDoan_Update : MyFromBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            lblNotice.Visible = false;

            CheckPermission();
            if (!IsPostBack)
            {
                Config();
                BindDataToControls();
            }



        }

        /// <summary>
        /// Thủ tục kiểm tra quyền sử dụng của người dùng có được quyền truy cập vào mục này hay không?
        /// </summary>
        private void CheckPermission()
        {
            Dictionary<string, int> Role = MyCommon.CheckPermission("SoLieuTinhHinhHoatDongCD");
            if (Role == null || Role["Xem"] == 0 || ((Role["Xem"] == 1 && Role["Sua"] == 0)))
            {
                Response.Redirect("Index.aspx?p=0");
            }
        }

        /// <summary>
        /// Thủ tục cấu hình giao diện, các controls
        /// </summary>
        private void Config()
        {
            //TODO
            txtThuTruongCoQuanChinhTri.Text = "";

        }

        /// <summary>
        /// Bind Data vào các contrls như Combobox, ListBox, textBox ...
        /// </summary>
        private void BindDataToControls()
        {
            //TODO
            if (Session["userid"] != null)
            {

                string sql = "Select DV.* " +
                             "From tblDonVi DV " +
                             "Inner Join tblUser U On U.DonViID=DV.ID " +
                             "Where U.ID=" + Session["userid"];
                DataTable dtDV = SqlHelper.ExecuteDataTable(CommandType.Text, sql);
                tdTenDonVi.InnerHtml = "<b>ĐƠN VỊ : </b>(" + dtDV.Rows[0]["MaDV"] + ") " + dtDV.Rows[0]["TenDV"];

                sql = "Select ID,TenDM From tblDanhMuc Where MaDM='" + MyConst.DanhMuc.KyBaoCao.Ma + "' ";
                SqlDataReader reKyBaoCao = SqlHelper.ExecuteReader(CommandType.Text, sql);
                clsDataBind.CreateCombo("Tất cả", reKyBaoCao, cboKyBaoCao, 1, 0);
            }


        }

        /// <summary>
        /// Bind Data vào gridview
        /// </summary>
        private void BindDataToGrid()
        {
            //TODO

            string sql = "Select BC.* From tblPhuLucSoLieuTinhHinhHoatDongCongDoan BC " +
                            "Inner Join tblDonVi DV On DV.ID=BC.DonViID " +
                            "Inner Join tblUser U On U.DonViID=DV.ID " +
                            "Where BC.IsCommon<>1 And U.ID=" + Session["userid"] + " And BC.KyBaoCaoID_DM=" + cboKyBaoCao.SelectedValue;
            grvData.DataSource = SqlHelper.ExecuteDataTable(System.Data.CommandType.Text, sql);
            grvData.DataBind();

            sql = "Select BC.* From tblPhuLucSoLieuTinhHinhHoatDongCongDoan BC " +
                         "Inner Join tblDonVi DV On DV.ID=BC.DonViID " +
                         "Inner Join tblUser U On U.DonViID=DV.ID " +
                         "Where BC.IsCommon=1 And U.ID=" + Session["userid"] + " And BC.KyBaoCaoID_DM=" + cboKyBaoCao.SelectedValue;
            DataTable temDT = SqlHelper.ExecuteDataTable(System.Data.CommandType.Text, sql);
            if (temDT.Rows.Count > 0)
            {
                txtTenBaoCao.Text = temDT.Rows[0]["CM_TenBaoCao"].ToString();
                txtThoiGianBaoCao.Text = temDT.Rows[0]["CM_ThoiGianBaoCao"].ToString();
                txtThuTruongCoQuanChinhTri.Text = temDT.Rows[0]["CM_ThuTruong"].ToString();
            }

            btnLuu.Visible = true;


        }

        #region Events

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            Response.Redirect("Index.aspx");
        }

        protected void cboKyBaoCao_SelectedIndexChanged(object sender, EventArgs e)
        {
            grvData.EditIndex = -1;
            BindDataToGrid();

            if (((DataTable)grvData.DataSource).Rows.Count == 0)
            {
                SqlTransaction MyTran = SqlHelper.BeginTransaction();
                try
                {
                    DataTable temDT = SqlHelper.ExecuteDataTable(CommandType.Text, "Select * From tblDanhMucMau8");
                    List<SqlParameter> lstParameter = new List<SqlParameter>();

                    string sSQL = "";
                    int sDVID = Convert.ToInt32(MyCommon.GetDonViID(Session["userid"].ToString()));
                    foreach (DataRow dr in temDT.Rows)
                    {
                        lstParameter.Clear();

                        lstParameter.Add(SqlHelper.CreateParameterObject("@STT", SqlDbType.NVarChar, ParameterDirection.Input, dr["STT"]));
                        lstParameter.Add(SqlHelper.CreateParameterObject("@DonViID", SqlDbType.Int, ParameterDirection.Input, sDVID));
                        lstParameter.Add(SqlHelper.CreateParameterObject("@KyBaoCaoID_DM", SqlDbType.Int, ParameterDirection.Input, cboKyBaoCao.SelectedValue));
                        lstParameter.Add(SqlHelper.CreateParameterObject("@NoiDungID_DM", SqlDbType.Int, ParameterDirection.Input, dr["ID"]));
                        lstParameter.Add(SqlHelper.CreateParameterObject("@NoiDung", SqlDbType.NVarChar, ParameterDirection.Input, dr["TenDM"]));
                        lstParameter.Add(SqlHelper.CreateParameterObject("@UserID", SqlDbType.Int, ParameterDirection.Input, Session["userid"]));


                        sSQL = "Insert Into tblPhuLucSoLieuTinhHinhHoatDongCongDoan(SoTT,DonViID, KyBaoCaoID_DM, NoiDungID_DM, NoiDung, CreateDate, UpdateDate, UserID, IsCommon) " +
                                "Values(@STT,@DonViID, @KyBaoCaoID_DM, @NoiDungID_DM, @NoiDung, GetDate(), GetDate(), @UserID,0)";
                        SqlHelper.ExecuteNonQuery(MyTran, CommandType.Text, sSQL, lstParameter);
                    }

                    lstParameter.Clear();

                    lstParameter.Add(SqlHelper.CreateParameterObject("@DonViID", SqlDbType.Int, ParameterDirection.Input, sDVID));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@KyBaoCaoID_DM", SqlDbType.Int, ParameterDirection.Input, cboKyBaoCao.SelectedValue));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@NoiDungID_DM", SqlDbType.Int, ParameterDirection.Input, 0));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@NoiDung", SqlDbType.NVarChar, ParameterDirection.Input, ""));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@UserID", SqlDbType.Int, ParameterDirection.Input, Session["userid"]));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@CM_TenBaoCao", SqlDbType.NVarChar, ParameterDirection.Input, "(Số liệu tính đến tháng     năm   20   )"));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@CM_ThoiGianBaoCao", SqlDbType.NVarChar, ParameterDirection.Input, "(Kèm theo báo cáo số:    ngày    tháng    năm 20...)"));



                    sSQL = "Insert Into tblPhuLucSoLieuTinhHinhHoatDongCongDoan(DonViID, KyBaoCaoID_DM, NoiDungID_DM, NoiDung, CreateDate, UpdateDate, UserID,CM_TenBaoCao, CM_ThoiGianBaoCao, IsCommon) " +
                            "Values(@DonViID, @KyBaoCaoID_DM, @NoiDungID_DM, @NoiDung, GetDate(), GetDate(), @UserID,@CM_TenBaoCao, @CM_ThoiGianBaoCao, 1)";
                    SqlHelper.ExecuteNonQuery(MyTran, CommandType.Text, sSQL, lstParameter);

                    MyTran.Commit();
                }
                catch(Exception ex)
                {
                    MyTran.Rollback();
                }
                grvData.EditIndex = -1;
                BindDataToGrid();
            }
        }

        protected void btnLuu_Click(object sender, EventArgs e)
        {

            string sSQL = "Update tblPhuLucSoLieuTinhHinhHoatDongCongDoan Set  CM_ThoiGianBaoCao=@CM_ThoiGianBaoCao, CM_ThuTruong=@CM_ThuTruong, CM_TenBaoCao=@CM_TenBaoCao " +
                            "Where IsCommon=1 And DonViID=" + MyCommon.GetDonViID(Session["userid"].ToString()) + " And KyBaoCaoID_DM=" + cboKyBaoCao.SelectedValue;

            List<SqlParameter> lstParameter = new List<SqlParameter>();
            lstParameter.Add(SqlHelper.CreateParameterObject("@CM_ThoiGianBaoCao", SqlDbType.NVarChar, ParameterDirection.Input, txtThoiGianBaoCao.Text.Trim()));
            lstParameter.Add(SqlHelper.CreateParameterObject("@CM_ThuTruong", SqlDbType.NVarChar, ParameterDirection.Input, txtThuTruongCoQuanChinhTri.Text.Trim()));
            lstParameter.Add(SqlHelper.CreateParameterObject("@CM_TenBaoCao", SqlDbType.NVarChar, ParameterDirection.Input, txtTenBaoCao.Text.Trim()));


            SqlHelper.ExecuteNonQuery(CommandType.Text, sSQL, lstParameter);

            lblNotice.Visible = true;
            lblNotice.InnerText = "Cập nhật thành công";

        }

        #endregion Events


        #region GridCommand

        protected void grvData_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            RowDataBound(sender, e);
        }

        protected void grvData_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            string sID = e.CommandArgument.ToString().Trim();
            string sCommandName = e.CommandName;


            if ((sCommandName == AIC_CD.MyConst.Action.Approve))
            {
                // cap nhat vao database
                TextBox txtDVT = (TextBox)grvData.Rows[grvData.EditIndex].FindControl("txtDVT");
                TextBox txtSoLuong = (TextBox)grvData.Rows[grvData.EditIndex].FindControl("txtSoLuong");
                TextBox txtTangGiam = (TextBox)grvData.Rows[grvData.EditIndex].FindControl("txtTangGiam");
                TextBox txtGhiChu = (TextBox)grvData.Rows[grvData.EditIndex].FindControl("txtGhiChu");

                string sSQL = "update tblPhuLucSoLieuTinhHinhHoatDongCongDoan set SoLuong = @SoLuong, TangGiam = @TangGiam, GhiChu= @GhiChu,DonViTinh = @DonViTinh, UpdateDate=GETDATE(), UserID = @UserID where ID = @ID";


                List<SqlParameter> lstParameter = new List<SqlParameter>();
                lstParameter.Add(SqlHelper.CreateParameterObject("@DonViTinh", SqlDbType.NVarChar, ParameterDirection.Input, txtDVT.Text.Trim()));
                lstParameter.Add(SqlHelper.CreateParameterObject("@SoLuong", SqlDbType.Float, ParameterDirection.Input, txtSoLuong.Text.Trim()));
                lstParameter.Add(SqlHelper.CreateParameterObject("@TangGiam", SqlDbType.Float, ParameterDirection.Input, txtTangGiam.Text.Trim()));
                lstParameter.Add(SqlHelper.CreateParameterObject("@GhiChu", SqlDbType.NVarChar, ParameterDirection.Input, txtGhiChu.Text.Trim()));
                lstParameter.Add(SqlHelper.CreateParameterObject("@UserID", SqlDbType.Int, ParameterDirection.Input, Session["userid"]));

                lstParameter.Add(SqlHelper.CreateParameterObject("@ID", SqlDbType.Int, ParameterDirection.Input, sID));

                SqlHelper.ExecuteNonQuery(CommandType.Text, sSQL, lstParameter);

                grvData.EditIndex = -1;
                BindDataToGrid();
                lblNotice.Visible = true;
                lblNotice.InnerText = "Cập nhật thành công";
            }
        }

        protected void grvData_RowEditing(object sender, GridViewEditEventArgs e)
        {
            grvData.EditIndex = e.NewEditIndex;
            BindDataToGrid();
        }

        protected void grvData_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            //grvData.PageIndex = e.NewPageIndex;
            //BindDataToGrid();
        }


        protected void grvData_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            grvData.EditIndex = -1;
            BindDataToGrid();
        }

        #endregion GridCommand
    }
}
